import { queryTemp } from '@/api/print/index.js'
import { gp } from '@gp'
import close from '@/assets/print_img/close.png'
import print from '@/assets/print_img/print.png'
import { localStorageConfig } from '@/config/localstorage.config'

const pkCorp = localStorage.getItem(localStorageConfig.__pkCorp)
let iframe, mRoot, mRootView, printIcon
export default async (templateCode, record) => {
  const param = {
    pageNum: 1,
    pageSize: 100,
    printTemplateCode: templateCode,
    printTemplateName: '',
    isUse: 0,
    pkCorp,
  }
  const { data = {} } = await queryTemp(param)
  const templateData = data?.list?.[0]
  if (!templateData) {
    gp.$baseMessage('打印配置有误!', 'error', 'vab-hey-message-error')
    return
  }
  let templateBody = null
  try {
    templateBody = JSON.parse(templateData['printTemplateBody'])
  } catch (error) {
    gp.$baseMessage('打印模板有误!', 'error', 'vab-hey-message-error')
    return
  }
  const printUrl = `/hiprint/print.html?a=` + Math.floor(Math.random() * 1000)
  const __print_root_view = document.body.querySelector('#__print_root')
  if (__print_root_view) {
    document.body.removeChild(__print_root_view)
  }

  // , printView, selectView
  //创建一个div
  mRootView = document.createElement('div')
  mRootView.id = '__print_root'
  mRoot = document.createElement('div')
  mRoot.style.border = '0'
  mRoot.style.position = 'absolute'
  mRoot.style.width = '60%'
  mRoot.style.height = '80%'
  mRoot.style.top = '-2000px' //10%
  mRoot.style.left = '20%'
  mRoot.style.zIndex = 9000
  mRoot.style.backgroundColor = '#fff'
  try {
    record = JSON.parse(JSON.stringify(record))
  } catch (error) {
    gp.$baseMessage('打印数据有误!', 'error', 'vab-hey-message-error')
    return
  }
  //创建一个 iframe
  iframe = document.createElement('iframe')
  iframe.style.width = '100%'
  iframe.style.height = '100%'
  iframe.style.border = '1px solid #f2f2f2'
  iframe.style.borderRadius = '4px'
  iframe.style.boxShadow = '5px 5px 10px #ccc'
  iframe.style.zIndex = 9001
  iframe.src = printUrl
  iframe.onload = function () {
    setTimeout(() => {
      // const mData = {
      //   type: 'hiprint',
      //   opt: 'setData',
      //   template: templateBody,
      //   record: record,
      // }
      // iframe.contentWindow.postMessage(mData, '*')

      toPrint(templateBody, record)
    }, 0)
  }
  mRoot.appendChild(iframe)
  addCloseIcon(mRootView, mRoot)
  addPrintIcon(mRoot, templateBody, record)
  mRootView.appendChild(mRoot)
  document.body.appendChild(mRootView)
}
//打印
const addPrintIcon = (mRoot, templateBody, record) => {
  printIcon = document.createElement('img')
  printIcon.src = print
  printIcon.style.zIndex = 9002
  printIcon.style.width = '25px'
  printIcon.style.height = '25px'
  printIcon.style.position = 'absolute'
  printIcon.style.top = '5px'
  printIcon.style.right = '45px'
  printIcon.onclick = () => {
    setTimeout(() => {
      toPrint(templateBody, record)
      //   const mData = {
      //     type: 'hiprint',
      //     opt: 'print',
      //     template: templateBody,
      //     record: record,
      //   }
      //   iframe.contentWindow.postMessage(mData, '*')
    }, 0)
  }
  mRoot.appendChild(printIcon)
}
const toPrint = (templateBody, record) => {
  setTimeout(() => {
    const mData = {
      type: 'hiprint',
      opt: 'print',
      template: templateBody,
      record: record,
    }
    iframe.contentWindow.postMessage(mData, '*')
  }, 0)
}
//关闭按钮
const addCloseIcon = (mRootView, mRoot) => {
  const closeIcon = document.createElement('img')
  closeIcon.src = close
  closeIcon.style.zIndex = 9002
  closeIcon.style.width = '25px'
  closeIcon.style.height = '25px'
  closeIcon.style.position = 'absolute'
  closeIcon.style.top = '5px'
  closeIcon.style.right = '5px'
  closeIcon.onclick = () => {
    // mRootView.removeChild(mRoot)
    // const __print_root_view = document.body.querySelector('#__print_root')
    // if (__print_root_view) {
    //   document.body.removeChild(__print_root_view)
    // }
    toClose()
  }
  mRoot.appendChild(closeIcon)
}
const toClose = () => {
  mRootView && mRootView.removeChild(mRoot)
  const __print_root_view = document.body.querySelector('#__print_root')
  if (__print_root_view) {
    document.body.removeChild(__print_root_view)
  }
  mRootView = null
}
